package com.hyy.lotterysystem.dao.mapper;

import com.hyy.lotterysystem.controller.param.InvitationParam;
import com.hyy.lotterysystem.dao.dataobject.ActivityDO;
import com.hyy.lotterysystem.service.enums.ActivityStatusEnum;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ActivityMapper {


    


    //把用户信息插入到活动人员关联表中（用户id和活动id同时存在不插入）
    @Insert("<script>" +
            "INSERT INTO activity_user (activity_id, user_id, user_name, status) " +
            "SELECT t.activity_id, #{userId}, #{userName}, 'INIT' " +
            "FROM (" +
            "<foreach collection='activityIds' item='activityId' separator='UNION ALL'>" +
            "SELECT #{activityId} as activity_id " +
            "</foreach>" +
            ") t " +
            "WHERE NOT EXISTS (" +
            "SELECT 1 FROM activity_user " +
            "WHERE activity_id = t.activity_id " +
            "AND user_id = #{userId}" +
            ")" +
            "</script>")
    int addUser(InvitationParam param);


    @Insert("insert into activity (activity_name,description,status,duration,create_user_id)" +
            " values " +
            "(#{activityName},#{description},#{status},#{duration},#{createUserId})")
    @Options(useGeneratedKeys = true,keyProperty = "id", keyColumn = "id")
    int insert(ActivityDO activityDO);


    @Select("select * from activity order by id desc limit #{offset}, #{pageSize}")
    List<ActivityDO> selectActivityList(@Param("offset") Integer offset,
                                        @Param("pageSize") Integer pageSize);


    @Select("select count(*) from activity")
    int count();

    @Select("select * from activity where id = #{id}")
    ActivityDO selectById(@Param("id") Long id);

    @Update("update activity set status = #{status} where id = #{id}")
    void updateStatus(@Param("id") Long id, @Param("status") String status);

    @Update("update activity set status = 'COMPLETED' where id = #{id}")
    void activityComplete(@Param("id") Long activityId);

    /*
    * 根据活动id查看活动窗台，活动持续时间扩展用*/
    @Select("select status from activity where id=#{id}")
    ActivityStatusEnum getActivityStatus(@Param("id") Long activityId);

    /**
     * 统计activity表中id在activityIds集合中的个数
     */
    @Select({
        "<script>",
        "SELECT COUNT(*) FROM activity WHERE id IN",
        "<foreach collection='activityIds' item='id' open='(' separator=',' close=')'>",
        "#{id}",
        "</foreach>",
        "</script>"
    })
    int countByIds(@Param("activityIds") List<Long> activityIds);

}
